// 得到今天、昨天、明天日期
// getDate(dates)  //dates为数字类型，0代表今日,-1代表昨日，1代表明日，返回yyyy-mm-dd格式字符串，dates不传默认代表今日。
let getDate = function (dates) {
	var dd = new Date();
	var n = dates || 0;
	dd.setDate(dd.getDate() + n);
	var y = dd.getFullYear();
	var m = dd.getMonth() + 1;
	var d = dd.getDate();
	m = m < 10 ? "0" + m : m;
	d = d < 10 ? "0" + d : d;
	var day = y + "-" + m + "-" + d;
	return day;
}


/**
 * @param startDate  开始日期 yyyy-MM-dd
 * @param enDate  结束日期 yyyy-MM-dd
 * @returns {number} 两日期相差的天数
 */
let getDaysBetween = function (startDate, enDate) {
	const sDate = Date.parse(startDate)
	const eDate = Date.parse(enDate)
	if (sDate > eDate) {
		return 0
	}
	if (sDate === eDate) {
		return 1
	}
	const days = (eDate - sDate) / (1 * 24 * 60 * 60 * 1000)
	return days
}

// 得到本月、上月、下月的起始、结束日期
// getMonth(type,months)  //type为字符串类型，有两种选择，"s"代表开始,"e"代表结束，months为数字类型，不传或0代表本月，-1代表上月，1代表下月
// getMonth("s",1)  //得到本月第一天的yyyy-mm-dd格式日期
// getMonth("e",1)  //得到本月最后一天的yyyy-mm-dd格式日期
let getMonth = function (type, months) {
	var d = new Date();
	var year = d.getFullYear();
	// var month = d.getMonth() + 1;
	var month = d.getMonth()+1;
	if (Math.abs(months) > 12) {
		months = months % 12;
	};
	if (months != 0) {
		if (month + months > 12) {
			year++;
			month = (month + months) % 12;
		} else if (month + months < 1) {
			year--;
			month = 12 + month + months;
		} else {
			month = month + months;
		};
	};
	month = month < 10 ? "0" + month : month;
	var date = d.getDate();
	var firstday = year + "-" + month + "-" + "01";
	var lastday = "";
	if (month == "01" || month == "03" || month == "05" || month == "07" || month == "08" || month == "10" || month ==
		"12") {
		lastday = year + "-" + month + "-" + 31;
	} else if (month == "02") {
		if ((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0)) {
			lastday = year + "-" + month + "-" + 29;
		} else {
			lastday = year + "-" + month + "-" + 28;
		};
	} else {
		lastday = year + "-" + month + "-" + 30;
	};
	var day = "";
	if (type == "s") {
		day = firstday;
	} else {
		day = lastday;
	};
	return day;
};

// 得到本周、上周、下周的起始、结束日期
// getMonday(type,dates)  //type为字符串类型，有两种选择，"s"代表开始,"e"代表结束，dates为数字类型，不传或0代表本周，-1代表上周，1代表下周
// getMonday("s",1)  //得到下周一的yyyy-mm-dd格式日期
// getMonday("e",1)  //得到下周日的yyyy-mm-dd格式日期
let getMonday = function (type, dates) {
	var now = new Date();
	var nowTime = now.getTime();
	var day = now.getDay();
	var longTime = 24 * 60 * 60 * 1000;
	var n = longTime * 7 * (dates || 0);
	if (type == "s") {
		var dd = nowTime - (day - 1) * longTime + n;
	};
	if (type == "e") {
		var dd = nowTime + (7 - day) * longTime + n;
	};
	dd = new Date(dd);
	var y = dd.getFullYear();
	var m = dd.getMonth() + 1;
	var d = dd.getDate();
	m = m < 10 ? "0" + m : m;
	d = d < 10 ? "0" + d : d;
	var day = y + "-" + m + "-" + d;
	return day;
};

// getYear(type,dates)  //type为字符串类型，有两种选择，"s"代表开始,"e"代表结束，dates为数字类型，不传或0代表今年，-1代表去年，1代表明年
// getYear("s",1)  //得到明年第一天的yyyy-mm-dd格式日期
// getYear("e",1)  //得到明年最后一天的yyyy-mm-dd格式日期
let getYear = function (type, dates) {
	var dd = new Date();
	var n = dates || 0;
	var year = dd.getFullYear() + Number(n);
	if (type == "s") {
		var day = year + "-01-01";
	};
	if (type == "e") {
		var day = year + "-12-31";
	};
	if (!type) {
		var day = year + "-01-01/" + year + "-12-31";
	};
	return day;
};

//获取当前时间  年-月-日 时-分-秒
let formatDate = function () {
	let date = new Date();
	let y = date.getFullYear(); // 年
	let MM = date.getMonth() + 1; // 月
	MM = MM < 10 ? ('0' + MM) : MM;
	let d = date.getDate(); // 日
	d = d < 10 ? ('0' + d) : d;
	let h = date.getHours(); // 时
	h = h < 10 ? ('0' + h) : h;
	let m = date.getMinutes(); // 分
	m = m < 10 ? ('0' + m) : m;
	let s = date.getSeconds(); // 秒
	s = s < 10 ? ('0' + s) : s;
	return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s;
	// return y + '-' + MM + '-' + d;
}

//格式化日对象 获取当前 年月日 星期天 时分秒
let getNowDate = function (){
  var date = new Date();
  var sign2 = ":";
  var year = date.getFullYear() // 年
  var month = date.getMonth() + 1; // 月
  var day = date.getDate(); // 日
  var hour = date.getHours(); // 时
  var minutes = date.getMinutes(); // 分
  var seconds = date.getSeconds() //秒
  var weekArr = [ '星期天','星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
  var week = weekArr[date.getDay()];
  // 给一位数的数据前面加 “0”
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  if (hour >= 0 && hour <= 9) {
    hour = "0" + hour;
  }
  if (minutes >= 0 && minutes <= 9) {
    minutes = "0" + minutes;
  }
  if (seconds >= 0 && seconds <= 9) {
    seconds = "0" + seconds;
  }
  // return year + "-" + month + "-" + day + " " + hour + sign2 + minutes + sign2 + seconds;
  return year + "-" + month + "-" + day + "  "+week +"  " + hour + sign2 + minutes + sign2 + seconds;
}

//获取上个月第一天和本月最后一天
let getDisabledDate = function () {
	const date = new Date()
	const year = date.getFullYear()
	const month = date.getMonth() //
	const day2 = new Date(year, month + 1, 0)
	const a = `${year}-${month}-01`
	const b = `${day2.getFullYear()}-${day2.getMonth() + 1}-${day2.getDate()}`
	const firstDay = new Date(a).getTime() // 上个月第一天
	const lastDay = new Date(b).getTime() // 本月最后一天
	return [stamp(firstDay), stamp(lastDay)]
}


// 时间戳转年月日 时分秒
let stamp = function (haoMiao) {
	//将毫秒转化成年月日时分秒
	var timdedetail = haoMiao*1000; //假如是10位毫秒需要乘1000
	//获取年份
	var year = new Date(timdedetail).getFullYear();
	//获取月份
	var month = new Date(timdedetail).getMonth() + 1;
	if (month < 10) {
		month = "0" + month;
	}
	//获取日
	var date = new Date(timdedetail).getDate();
	if (date < 10) {
		date = "0" + date;
	}
	//获取小时
	var h1 = new Date(timdedetail).getHours() < 10 ? '0' + new Date(timdedetail).getHours() : new Date(timdedetail)
		.getHours();
	//获取分钟
	var m1 = new Date(timdedetail).getMinutes() < 10 ? '0' + new Date(timdedetail).getMinutes() : new Date(timdedetail)
		.getMinutes();
	//获取秒
	var s1 = new Date(timdedetail).getSeconds() < 10 ? '0' + new Date(timdedetail).getSeconds() : new Date(timdedetail)
		.getSeconds();
	//组合格式为年-月-日 时：分：秒（2021-07-05 21:21:21）
	var starttime = year + "-" + month + "-" + date + " " + h1 + ":" + m1 + ":" + s1;
	//输出样式：2020-10-25 14:25:36
	return starttime
}

// 时间戳转年月日
let getDay = function (haoMiao) {
	//将毫秒转化成年月日时分秒
	var timdedetail = haoMiao*1000; //假如是10位毫秒需要乘1000
	//获取年份
	var year = new Date(timdedetail).getFullYear();
	//获取月份
	var month = new Date(timdedetail).getMonth() + 1;
	if (month < 10) {
		month = "0" + month;
	}
	//获取日
	var date = new Date(timdedetail).getDate();
	if (date < 10) {
		date = "0" + date;
	}
	//获取小时
	var h1 = new Date(timdedetail).getHours() < 10 ? '0' + new Date(timdedetail).getHours() : new Date(timdedetail)
		.getHours();
	//获取分钟
	var m1 = new Date(timdedetail).getMinutes() < 10 ? '0' + new Date(timdedetail).getMinutes() : new Date(timdedetail)
		.getMinutes() < 10;
	//获取秒
	var s1 = new Date(timdedetail).getSeconds() < 10 ? '0' + new Date(timdedetail).getSeconds() : new Date(timdedetail)
		.getSeconds();
	//组合格式为年-月-日 时：分：秒（2021-07-05 21:21:21）
	var starttime = year + "-" + month + "-" + date ;
	//输出样式：2020-10-25 
	return starttime
}


//判断是否为闰年 是返回1
let isLeap = function (year) {
	if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
		return 1;
	}
	return 0;
}

// 近n月  如：getRecentMonth(1)  返回近1个月的日期
//        如：getRecentMonth(-1)  返回后1个月的日期
let getRecentMonth = function (n) {
	let result = '';
	const datenow = new Date();
	const dateend = datenow.getFullYear().toString() + '-' + (datenow.getMonth() + 1).toString() + '-' + datenow
		.getDate().toString();
	datenow.setMonth(datenow.getMonth() - n);
	let dyear = datenow.getFullYear();
	let dmonth = datenow.getMonth() + 1;
	dmonth = dmonth < 10 ? '0' + dmonth : dmonth;
	let dday = datenow.getDate();
	dday = dday < 10 ? '0' + dday : dday;
	const datestart =
		dyear.toString() + '-' + dmonth.toString() + '-' + dday.toString();
	result = [datestart, dateend];
	return result;
}

// 近n天 getRecentDay_Date(-n)  返回近n天日期范围
// 近n天 getRecentDay_Date(n)  返回后n天日期范围
let getRecentDay_Date = function (n) {
	var dd = new Date();
	dd.setDate(dd.getDate() + n); //获取n天后的日期
	var y = dd.getFullYear();
	var m = dd.getMonth() + 1; //获取当前月份的日期
	var d = dd.getDate();
	let day = y + '-' + m + '-' + d;
	// console.log(day);
	return day;
}

//两个时间段的年月数组   yyyy-mm
let getDateArry = function (startDay, endDay) {
    var d1 = startDay;
    var d2 = endDay;
    var dateArry = new Array();
    var s1 = d1.split("-");
    var s2 = d2.split("-");
    var mCount = 0;
    if (parseInt(s1[0]) < parseInt(s2[0])) {
        mCount = (parseInt(s2[0]) - parseInt(s1[0])) * 12 + parseInt(s2[1]) - parseInt(s1[1]) + 1;
    } else {
        mCount = parseInt(s2[1]) - parseInt(s1[1]) + 1;
    }
    if (mCount > 0) {
        var startM = parseInt(s1[1]);
        var startY = parseInt(s1[0]);
        for (var i = 0; i < mCount; i++) {
            if (startM < 12) {
                dateArry[i] = startY + "-" + (startM > 9 ? startM : "0" + startM);
                startM += 1;
            } else {
                dateArry[i] = startY + "-" + (startM > 9 ? startM : "0" + startM);
                startM = 1;
                startY += 1;
            }
        }
    }
    return dateArry;
}

//2个日期中间的所有日期
let getDatelist = function (startTime, endTime) {
	//初始化日期列表，数组
	var allDate = new Array();
	var i = 0;
	//开始日期小于等于结束日期,并循环
	while (startTime <= endTime) {
		allDate[i] = startTime;
		//获取开始日期时间戳
		var startTime_ts = new Date(startTime).getTime();
		//增加一天时间戳后的日期
		var next_date = startTime_ts + 24 * 60 * 60 * 1000;
		//拼接年月日，这里的月份会返回（0-11），所以要+1
		var next_dates_y = new Date(next_date).getFullYear() + "-";
		var next_dates_m =
			new Date(next_date).getMonth() + 1 < 10 ?
			"0" + (new Date(next_date).getMonth() + 1) + "-" :
			new Date(next_date).getMonth() + 1 + "-";
		var next_dates_d =
			new Date(next_date).getDate() < 10 ?
			"0" + new Date(next_date).getDate() :
			new Date(next_date).getDate();
		startTime = next_dates_y + next_dates_m + next_dates_d;
		//增加数组key
		i++;
	}
	return allDate
}

export {
	getDate,
	getDaysBetween,
	getMonth,
	getMonday,
	getYear,
	formatDate,
	getNowDate,
	getDisabledDate,
	stamp,
	isLeap,
	getRecentMonth,
	getRecentDay_Date,
	getDateArry,
	getDatelist,
	getDay
}